package com.example.mybatisdemo.mapper;

import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //查询所有的用户信息
//@Select("select id, username, password, age, gender, phone, " +
//        "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
//    @Results(id = "BassMap",value = {
//            @Result(column = "delete_flag",property = "deleteFlag"),
//            @Result(column = "create_time",property = "createTime"),
//            @Result(column = "update_time",property = "updateTime"),
//
//    })
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from user_info")
    List<UserInfo> selectAll();

//    @ResultMap("BassMap")
    @Select("select id, username, password, age, gender, phone, " +
            "delete_flag, create_time, update_time from user_info where id = #{aa}")
    UserInfo selectUserById(Integer id);

    //非唯一主键查询，返回结果建议使用List
    @Select("select *from user_info where age = #{age} and gender = #{g ender}")//推荐
//    @Select("select *from user_info where age = #{param1} and gender = #{param2}")

    List<UserInfo> selectUserByAgeAndGender(Integer age,Integer gender);
//    List<UserInfo> selectUserByAgeAndGender(@Param("gender") Integer gender, @Param("age") Integer age);

    //传递对象
    @Insert("insert into user_info (username, password, age, gender)" +
    "values (#{username}, #{password}, #{age}, #{gender})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, password, age, gender)" +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})")
    Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);

    //删除数据
    @Delete("delete from user_info where id = #{id}")
    Integer delete(Integer id);

    //修改数据
    @Update("update user_info set password = #{password} where id = #{id}")
    Integer update(Integer id,String password);

//    @Update("update user_info set password = #{password} where id = #{id}")
//    Integer update(UserInfo userInfo);


    //排序功能
    @Select("select *from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);


//    @Select("select *from user_info where username like '%${name}%'")
//    List<UserInfo> selectUserByLike(String name);

    @Select("select *from user_info where username like CONCAT('%',#{name},'%')")
    List<UserInfo> selectUserByLike(String name);




    @Insert("<script>" +
            "insert into user_info\n" +
            "<trim prefix=\"(\" suffixOverrides=\",\" suffix=\")\">" +
            "<if test=\"username != null\"> username ,</if>" +
            "<if test=\"password != null\"> password ,</if>" +
            "<if test=\"age != null\"> age ,</if>" +
            "<if test=\"gender != null\"> gender ,</if>" +
            "</trim> values" +
            "<trim prefix=\"(\" suffixOverrides=\",\" suffix=\")\">" +
            "<if test=\"username != null\"> #{username} ,</if>" +
            "<if test=\"password != null\"> #{password} ,</if>" +
            "<if test=\"age != null\"> #{age} ,</if>" +
            "<if test=\"gender != null\"> #{gender} ,</if>" +
            "</trim>" +
            "</script>")
    Integer insertUserByCondition(UserInfo userInfo);
}
